package net.xumf.support.enums;

import lombok.Getter;
import net.xumf.support.exception.NotFoundEnumInfoException;

import java.util.Arrays;

@Getter
public enum CodeEnum {

    FAILURE(400, "操作失败"),
    SUCCESS(200, "操作成功"),
    SYSTEM_LOG_INSERT_ERROR(10000, "系统日志录入异常"),
    ENUM_NOT_FOUND_ERROR(10001, "没有找到相应枚举"),
    USER_NOT_FOUND_ERROR(10002, "用户不存在"),
    USER_NOT_FOUND_LOCK(10003, "用户被锁定"),
    USER_PASS_ERROR(10004, "账号密码组合错误"),
    USER_LOGIN_ERROR(10005, "登录失败，请重新登录！"),
    USER_LOGIN_INVALID(10006, "登录失效，请重新登录！"),
    UNAUTHORIZED(10007, "操作失败，权限不足！"),
    MAIL_AUTHENTICATION_FAIL(10008, "邮箱认证失败！"),
    REPORT_NOT_FOUND_ERROR(10009, "邮箱认证失败！"),
    REQUEST_LIMIT_EXCEED_ERROR(10010, "用户请求接口超过限制次数！"),
    NOT_REQUEST_ERROR(10011, "HttpServletRequest对象不存在！"),
    PARAM_SETTING_ERROR(10012, "参数配置错误！"),
    SQL_ACTION_ERROR(10013, "系统内部错误！"),
    REQUEST_PARAM_ERROR(10014, "请求参数有误！"),
    OLD_PASSWORD_ERROR(10015, "原密码不匹配错误！"),
    TOKEN_INVALID(10016, "登录令牌失效！"),
    TOKEN_ILLEGAL(10017, "非法登录令牌！"),
    NOT_AUTH(10020, "无权访问！"),

    MODIFY_NOT_FOUND_ROW_ERROR(20006, "更新数据失败，无法找到对应数据！"),
    QUERY_NOT_FOUND_ROW_ERROR(20007, "查询数据失败，无法找到对应数据！"),
    DELETE_NOT_FOUND_ROW_ERROR(20007, "删除数据失败，无法找到对应数据！"),

    MODIFY_SUCCESS(20010, "更新数据成功！"),
    DELETE_SUCCESS(20011, "删除数据成功！"),

    TEST_CONNECTION_SUCCESS(30007, "连接成功！"),
    TEST_CONNECTION_FAIL(30008, "连接失败！"),

    QUARTZ_ALREADY_EXISTS(40000, "定时器名称重复错误!");

    private int code;
    private String msg;

    CodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static CodeEnum getByCode(int code) {

        return Arrays.stream(values()).filter(o -> o.code == code).findAny()
                .orElseThrow(() -> new NotFoundEnumInfoException(CodeEnum.ENUM_NOT_FOUND_ERROR));
    }
}
